using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;

namespace Concurrente.Lab9
{
    class Program
    {
        static int D;

        //static void Main(string[] args)
        //{
        //    Console.WriteLine("D = " + D);

        //    Thread pA = new Thread(ProcesoA);
        //    Thread pB = new Thread(ProcesoB);
        //    pA.Start();
        //    pB.Start();

        //    //Console.ReadLine();
        //    Console.WriteLine("D = " + D);
        //    Console.ReadLine();
        //}

        static void Main(string[] args)
        {
            while(true)
                MainX(args);
        }

        static Thread pA;
        static Thread pB;
        
        static void MainX(string[] args)
        {
            D = 20;
            Console.WriteLine("D = " + D);

            pA = new Thread(ProcesoA);
            pB = new Thread(ProcesoB);
            
            pA.Start();
            pB.Start();

            //Console.ReadLine();
            Console.WriteLine("D = " + D);
            Console.ReadLine();
        }

        public static void ProcesoA()
        {
            Random r = new Random();
            Thread.Sleep(r.Next(1, 1000));

            int peticion = 10;
            
            D = D - peticion;
            
            Console.WriteLine("Proceso A: D = " + D);
            pA.Abort();
        }

        public static void ProcesoB()
        {
            Random r = new Random();
            Thread.Sleep(r.Next(1, 1000));

            int peticion = 10;

            D = D - peticion;

            Console.WriteLine("Proceso B: D = " + D);
            pB.Abort();
        }
    }
}
